package com.tequila.core.service.impl;

import com.google.common.collect.Lists;
import com.tequila.core.dao.CardDao;
import com.tequila.core.service.CardService;
import com.tequila.domain.dto.CardDto;
import com.tequila.domain.entity.Card;
import com.tequila.domain.query.CardQuery;
import fe.core.Assert;
import fe.core.BeanHelper;
import fe.core.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    public static final Logger logger = LoggerFactory.getLogger(CardServiceImpl.class);

    @Resource
    private CardDao cardDao;

    @Override
    public Pagination<CardDto> queryPageListByParam(CardQuery query) {
        Assert.assertNotNull(query);
        int total = cardDao.queryPageCountByParam(query);

        List<CardDto> list = Lists.newArrayList();
        if (total > 0) {
            list = cardDao.queryPageListByParam(query);
        }
        return new Pagination<>(query, list, total);
    }

    @Override
    public CardDto queryById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        Card card = cardDao.queryById(id);
        CardDto cardDto = new CardDto();
        BeanHelper.copyProperties(cardDto,card);
        return cardDto;
    }

    @Override
    public List<CardDto> queryByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return cardDao.queryByIdList(idList);
    }

    @Override
    public List<CardDto> queryListByParam(CardQuery query) {
        Assert.assertNotNull(query,"query不能为空!");
        return cardDao.queryListByParam(query);
    }


    @Override
    public Long insert(Card entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        cardDao.insert(entity);
        return entity.getId();
    }

    @Override
    public Long save(Card entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        cardDao.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public int updateById(Card param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return cardDao.updateById(param);
    }

    @Override
    public int updateSelectiveById(Card param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return cardDao.updateSelectiveById(param);
    }

    @Override
    public int deleteById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        return cardDao.deleteById(id);
    }

    @Override
    public int deleteByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return cardDao.deleteByIdList(idList);
    }
}
